//Application Window Component Constructor
function RootView() {
    var self = Titanium.UI.createWindow({
        backgroundColor: 'black',
        fullscreen: true,
    });
    var Hompage = require('view/OptionView');
    var Func1 = require('view/LeaseRentalView');
    var Func2 = require('view/SubFunc2View');
    var Func22 = require('view/SellerStampDutyView');
    var Func21 = require('view/BuyerStampDutyView');
    var Func3 = require('view/Mortgage');
    var PKTextField = require('view/PickerTextField');
    var osname = Ti.Platform.osname;
    var screenWidth = Ti.Platform.displayCaps.platformWidth;
    var screenHeight = Ti.Platform.displayCaps.platformHeight;
    var headerHeight = '15%';
    var btnRectUnit = 40;
    var btnRectPadding = 5;
    var enumHompage = 0;
    var enumFunc1 = 1;
    var enumFunc2 = 2;
    var enumFunc21 = 4;
    var enumFunc22 = 5;
    var enumFunc3 = 3;
    var currView = null;
    // Create header view
    var vHeader = Titanium.UI.createView({
        backgroundColor: 'gray',
        top: 0,
        left: 0,
        width: screenWidth,
        height: headerHeight
    });
    self.add(vHeader);
    var functionTitles = ['Property Rental: Fixed Rent', 'Main Func2', 'Buyer\'s Stamp Duty', 'Seller\'s Stamp Duty', 'Mortgage', 'Home'];
    var functionIds = [enumFunc1, enumFunc2, enumFunc21, enumFunc22, enumFunc3, enumHompage];
    var rightTitles = ['Home', 'Property Rental: Fixed Rent', 'Main Func2', 'Mortgage', 'Buyer\'s Stamp Duty', 'Seller\'s Stamp Duty'];
    var pkSelectData = new Array();
    for (var i = 0; i < functionTitles.length; i++) {
        pkSelectData[i] = {
            title: functionTitles[i],
            valueId: functionIds[i]
        };
        Ti.API.info('insert: ' + functionTitles[i] + '-->' + functionIds[i]);
    };
    var txtSelectView = new PKTextField(self, 1, pkSelectData);
    txtSelectView.addEventListener('pickerTextFieldChange', function (e) {
        if (osname === 'android') {
            changeView(e.valueId);
        } else {
            txtSelectView.fireEvent('closeKeyBoard', {});
            changeView(e.valueId);
        };
    });
    txtSelectView.width = '90%';
    // Add to the parent view.
    vHeader.add(txtSelectView);

    function changeView(func) {
        if (currView) {
            self.remove(currView);
            currView = null;
        };
        // txtSelectView.visible = !((func === enumHompage) || (func === enumFunc2));
        txtSelectView.fireEvent('changeIndexSelected', {
            indexSelected: functionIds.indexOf(func)
        });
        switch (func) {
            case enumHompage:
                currView = new Hompage(screenWidth);
                break;
            case enumFunc1:
                currView = new Func1(screenWidth);
                break;
            case enumFunc2:
                currView = new Func2(screenWidth);
                break;
            case enumFunc21:
                currView = new Func21(screenWidth);
                break;
            case enumFunc22:
                currView = new Func22(screenWidth);
                break;
            case enumFunc3:
                currView = new Func3(screenWidth);
                break;
        }
        if (currView) {
            currView.top = headerHeight;
            self.add(currView);
        };
    }
    Ti.App.addEventListener('chooseFunction', function (e) {
        self.remove(currView);
        currView = null;
        changeView(e.info);
    });
    changeView(enumHompage);
    return self;
};
module.exports = RootView;